#!/bin/bash

###
### Install Golimojo as a "service" on a RedHat compatible Linux.
###

# Source our bash_profile in case we're being run in a non-login shell...
. ~/.bash_profile

# Extract the parameters into named variables.
SERVICE_STAGING_FOLDER=$1

###
### Determine where to put the actual running service folder.
###

# Specify the folder of service folders.
SERVICE_MASTER_FOLDER=`(cd $SERVICE_STAGING_FOLDER/..; pwd)`

# Specify the prefix for the service folder.
SERVICE_FOLDER_PREFIX="golimojo"

# Determine the next service folder version number.
SERVICE_FOLDER_VERSION_NUM=$((cd $SERVICE_MASTER_FOLDER/../golimojo; ls -d $SERVICE_FOLDER_PREFIX-*) | awk \
	-W 'source=BEGIN {max = -1; FS="-"}' \
	-W 'source=/^golimojo-[0-9]+$/ {num = $2 + 0; if (num > max) max = num}' \
	-W 'source=END {print max + 1}')

# Specify the actual service folder.
SERVICE_FOLDER=$SERVICE_MASTER_FOLDER/$SERVICE_FOLDER_PREFIX-$SERVICE_FOLDER_VERSION_NUM
echo $SERVICE_FOLDER

###
### Copy the service staging folder to the running service folder
###

cp -r $SERVICE_STAGING_FOLDER $SERVICE_FOLDER

###
### Update the classpath in the service config file.
###

#-----sed "s#\(wrapper\.java\.classpath\.[0-9][0-9]*=\)[^\.].*/#\1$SERVICE_FOLDER/lib/#" < $SERVICE_FOLDER/wrapper.conf > $SERVICE_FOLDER/wrapper.conf.1
#-----sed "s#\(wrapper\.java\.classpath\.[0-9][0-9]*=\)\./#\1$SERVICE_FOLDER/#" < $SERVICE_FOLDER/wrapper.conf.1 > $SERVICE_FOLDER/wrapper.conf.2
#-----rm $SERVICE_FOLDER/wrapper.conf.1
#-----mv $SERVICE_FOLDER/wrapper.conf.2 $SERVICE_FOLDER/wrapper.conf


### ...................................
### Write the shell config file
### ...................................

JREPATH=$(which java)
JREPATH=${JREPATH%/*}

JDKPATH=$(which jar)
JDKPATH=${JDKPATH%/*}

GCLASSPATH=$(echo $(find "$SERVICE_FOLDER/lib" -name "*.jar") | sed "s/ /:/g")

echo "# Machine generated.  Edit at your own peril." >  "$SERVICE_FOLDER/golimojo.config.sh"
echo "JREPATH='$JREPATH'"                            >> "$SERVICE_FOLDER/golimojo.config.sh"
echo "JDKPATH='$JDKPATH'"                            >> "$SERVICE_FOLDER/golimojo.config.sh"
echo "JAVA_HOME='$JAVA_HOME'"                        >> "$SERVICE_FOLDER/golimojo.config.sh"
echo "GCLASSPATH='$GCLASSPATH'"                      >> "$SERVICE_FOLDER/golimojo.config.sh"

#
#
#

# Specify the location/name of the startup script.
SERVICE="golimojo-service.sh"
STARTUP_SCRIPT=/etc/init.d/$SERVICE
ls $STARTUP_SCRIPT

# Kill the previous service -- if any -- and remove the link from /etc/init.d
if test -L $STARTUP_SCRIPT; then 
	echo "Shutting down existing service and disabling it."
	$STARTUP_SCRIPT stop;
	chkconfig $SERVICE off
	rm $STARTUP_SCRIPT;
fi

# Link the startup script into the init.d directory and register it with chkconfig.
ln -s $SERVICE_FOLDER/golimojo-service.sh $STARTUP_SCRIPT
chkconfig $SERVICE on

# Start the service in the background.
$STARTUP_SCRIPT start

# Use tail to display the log during startup.
echo
echo o
echo 
echo o
echo
echo o
echo
echo Service startup initiated -- ^C will now only stop log display
echo
sleep 3
cat $SERVICE_FOLDER/stdxxx.log
tail -f $SERVICE_FOLDER/stdxxx.log

